/**
* All Rights Reserved , Copyright (C) 2022 , 青岛鼎信通讯股份有限公司
*
* PmsProvinceCityMatchInstanceService
* 省份地市信息实例表
*
* 修改纪录
* 2022-03-25 版本：1.0 xinfushuo 创建。
* @version 版本：1.0
* @author 作者：xinfushuo
* 创建日期：2022-03-25
*/
package com.topscomm.mms.service.impl;

import com.topscomm.basic.util.PrimaryKeyUtil;
import com.topscomm.mms.MmsSystemConst;
import com.topscomm.mms.pojo.MmsProvinceCityMatchinfoEntity;
import com.topscomm.mms.pojo.PmsProvinceCityMatchInstanceEntity;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.tap.common.ParameterCache;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.topscomm.mms.service.IPmsProvinceCityMatchInstanceService;
import com.topscomm.mms.service.impl.auto.PmsProvinceCityMatchInstanceServiceAuto;

import java.time.LocalDate;
import java.time.YearMonth;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

@Service(value = "pmsProvinceCityMatchInstanceService")
@Scope(value = "singleton")
public class PmsProvinceCityMatchInstanceService extends PmsProvinceCityMatchInstanceServiceAuto implements IPmsProvinceCityMatchInstanceService {
	/**
	  * @description:
	  * @author xinfushuo
	  * @param
	  * @throws
	  * @return
	  * @date 2022/3/31
	  * @modify
	  */
    @Override
    public PmsProvinceCityMatchInstanceEntity assembleEntity(Long parentid, String periodMonth, MmsProvinceCityMatchinfoEntity mmsProvinceCityMatchinfoEntity){
        PmsProvinceCityMatchInstanceEntity entity  = new PmsProvinceCityMatchInstanceEntity();
        long[] arrayId = PrimaryKeyUtil.GeneIDsFromCache(PmsProvinceCityMatchInstanceEntity.tableName, 1);
        entity.setPeriodmonth(periodMonth);
        entity.setParentid(parentid);
        entity.setId(arrayId[0]);

        entity.setDeptcommoncode(mmsProvinceCityMatchinfoEntity.getDeptcommoncode());
        entity.setName(mmsProvinceCityMatchinfoEntity.getName());
        entity.setNamecode(mmsProvinceCityMatchinfoEntity.getNamecode());
        entity.setArealevel(mmsProvinceCityMatchinfoEntity.getArealevel());
        entity.setCode(mmsProvinceCityMatchinfoEntity.getCode());
        entity.setLevelcode(mmsProvinceCityMatchinfoEntity.getLevelcode());
        entity.setAreaauthorizationusercode(mmsProvinceCityMatchinfoEntity.getAreaauthorizationusercode());
        entity.setAreaauthorizationusername(mmsProvinceCityMatchinfoEntity.getAreaauthorizationusername());
        entity.setParentname(mmsProvinceCityMatchinfoEntity.getParentname());
        entity.setAreatype(mmsProvinceCityMatchinfoEntity.getAreatype());
        entity.setUseful(mmsProvinceCityMatchinfoEntity.getUseful()
                );
        entity.setUsercode(mmsProvinceCityMatchinfoEntity.getUsercode());
        entity.setUsername(mmsProvinceCityMatchinfoEntity.getUsername());
        entity.setDeptcode(mmsProvinceCityMatchinfoEntity.getDeptcode());
        entity.setDeptname(mmsProvinceCityMatchinfoEntity.getDeptname());
        entity.setRealarea(mmsProvinceCityMatchinfoEntity.getRealarea());
        entity.setAreamatch(mmsProvinceCityMatchinfoEntity.getAreamatch());
        entity.setSortCode(mmsProvinceCityMatchinfoEntity.getSortcode());
        entity.setEnabled(mmsProvinceCityMatchinfoEntity.getEnabled());
        entity.setCreateuser(mmsProvinceCityMatchinfoEntity.getCreateuser());
        entity.setModifieduser(mmsProvinceCityMatchinfoEntity.getModifieduser());
        entity.setCreateorgid(mmsProvinceCityMatchinfoEntity.getCreateorgid());

        entity.setCreateon(mmsProvinceCityMatchinfoEntity.getCreateon());
        entity.setModifiedon(mmsProvinceCityMatchinfoEntity
                .getModifiedon());
        return entity;
    }
   @Override
   public void  syncDisabled(MmsProvinceCityMatchinfoEntity mmsProvinceCityMatchinfoEntity){
        String levelCode = mmsProvinceCityMatchinfoEntity.getLevelcode();
       int  PmsDate = ConvertUtil.convertToInt(ParameterCache.getValue(MmsSystemConst.PmsProvinceCityUpdateDate));
       //约定日期之前 填本月 约定日期之后 填下一个月
       LocalDate now = LocalDate.now();
       if(now.getDayOfMonth()>PmsDate){
           now = now.plusMonths(1);
       }
       String nowString = YearMonth.of(now.getYear(),now.getMonth()).toString();
       StringBufferProxy sql = new StringBufferProxy();
       sql.appendSingle("levelcode  = '{0}'",levelCode);
       sql.appendSingle("and TO_DATE(PERIODMONTH,'yyyy-mm') >=  TO_DATE('{0}','yyyy-mm') ",nowString);
       this.deleteByWhere(sql.toString() );


   }
   @Override
   public void syncAuthUser(String levelcode, String authCode, String authName){
       Map<String,Object> updateMap = new HashMap<>();
       updateMap.put(PmsProvinceCityMatchInstanceEntity.FieldAreaauthorizationusercode,authCode);
       updateMap.put(PmsProvinceCityMatchInstanceEntity.FieldAreaauthorizationusername,authName);
       this.updateByWhere(updateMap,"levelcode = '".concat(levelcode).concat("'"));
   }
}
